/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.posting;

import domain.DomainController;
import java.util.logging.Level;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class PostBodyPanel extends javax.swing.JPanel
{

    /** Creates new form PostBodyPanel */
    public PostBodyPanel()
    {
        initComponents();
        setUndoManager();
    }

    private void setUndoManager()
    {
        txtPost.getDocument().addUndoableEditListener(
            new UndoableEditListener()
            {
                public void undoableEditHappened(UndoableEditEvent e)
                {
                    DomainController.getInstance().getUndoManager().addEdit(e.getEdit());
                    DomainController.getInstance().updateUndoRedoUI();
                }
            }
        );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bBCodePanel1 = new gui.posting.BBCodePanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtPost = new javax.swing.JTextArea();

        setLayout(new java.awt.BorderLayout());
        add(bBCodePanel1, java.awt.BorderLayout.PAGE_START);

        txtPost.setColumns(20);
        txtPost.setLineWrap(true);
        txtPost.setRows(5);
        txtPost.setWrapStyleWord(true);
        jScrollPane1.setViewportView(txtPost);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private gui.posting.BBCodePanel bBCodePanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtPost;
    // End of variables declaration//GEN-END:variables

    public String getText()
    {
        return this.txtPost.getText();
    }

    public void postRequestFocus()
    {
        this.txtPost.requestFocus();
    }

    public void setText(String text)
    {
        this.txtPost.setText(text);
        this.txtPost.requestFocus();
    }

    public void setCaretPosition(int i)
    {
        this.txtPost.setCaretPosition(i);
    }

    public void setButtonsEnabled(boolean enabled)
    {
        this.bBCodePanel1.setButtonsEnabled(enabled);
        this.txtPost.setEnabled(enabled);
    }

    public void insertTagInPostBody(String tag)
    {
        String selectedText = this.getSelectedTextInPost();

        if(selectedText == null)
        {
            this.doInsert(tag);
        }
        else
        {
            try
            {
                //Text selected...
                //Remove selected text
                int start = this.txtPost.getSelectionStart();
                int length = selectedText.length();
                this.txtPost.getDocument().remove(start, length);
                //insert
                this.doInsert(tag);
            }
            catch(Exception e)
            {
                DomainController.getInstance().getLogger().log(Level.WARNING, "Cannot insert tag! {0}", e.getMessage());
            }
        }
    }

    private void doInsert(String tag)
    {
        int caret = this.txtPost.getCaretPosition();

        String text = this.txtPost.getText();

        text = text.substring(0, caret) + tag + text.substring(caret);

        this.txtPost.setText(text);

        this.txtPost.requestFocus();
        this.txtPost.setCaretPosition(caret + tag.length());
    }

    public String getSelectedTextInPost()
    {
        return this.txtPost.getSelectedText();
    }

    public void clearPost()
    {
        this.setText("");
        this.bBCodePanel1.clearPost();
    }
}
